import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config} from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-sensor-fault-track',
	templateUrl: './sensor-fault-track.component.html',
})
export class SensorFaultTrackComponent implements OnInit {
	tableData:any= []; //table数据
	resultTableData:any = [];//客户端分页数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize :number= 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	selectedCompanyId:string = ''; //公司ID
	selectedCompanyName:string = ''; //公司名称
	carNumber:string = '';// 车牌号码
	deviceId:string = '';//设备ID
	tableType:string = "1";//统计类型  1:首次安装后故障统计 2:维修后故障统计
	useTypeL2List:any = carTypeListObj.carDeatailUseList;
	constructor(private jsonApiService: JsonApiService, private router: Router,private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
	}

	ngOnInit() {
//		this.load();		
	}
	ngOnDestroy(){
		document.onclick = null;
	}
	//公司选中回调
    companySelected(data){
        this.selectedCompanyId = data.companyId;
        this.selectedCompanyName = data.companyName;
        this.carNumber = data.carNumber;
        this.deviceId = data.deviceId;
    }
	//表格搜索
	tableSearch() {
		this.curPage=1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//刷新
	refresh(){
    	this.load();
  	}
	// 分页查询
	load() {
	    if(!this.selectedCompanyId){
	        this.jsonApiService.message('请选择需要查看的公司名称');
	        this.tableData = [];
	        this.totalCount = 0;
	        this.resultTableData = [];
	        return
	    }
		let reqdata = {
			type: this.tableType,
			companyId: this.selectedCompanyId,
			deviceId: this.deviceId,
		}
		this.jsonApiService.http_obj("projectOperations/sensorBadTrack.html", reqdata).subscribe(data => {
		    if(data.success){
		        for(let item of data.data){
                    item.useTypeL2Name = this.useTypeL2List[item.useTypeL2];
		        }
		        this.tableData = data.data;
                this.totalCount = data.data.length;
                this.client_pages(0,this.pageSize);
		    }
		})
	}
	// 列表导出
	tableExport(){
	    if(!this.selectedCompanyId){
            this.jsonApiService.message('请选择需要导出数据的公司名称')
            return
        }
	    var form = $("<form>"); //定义一个form表单
        form.attr("style", "display:none");
        form.attr("target", "");
        form.attr("method", "post");
        form.attr("action", config.BaseURL + 'projectOperations/exportSensorBadTrack.html');
        var fileInput1 = $("<input>");
        fileInput1.attr("type", "hidden");
        fileInput1.attr("name", "companyId"); //设置属性的名字
        fileInput1.attr("value", this.selectedCompanyId); //设置属性的值
        var fileInput2 = $("<input>");
        fileInput2.attr("type", "hidden");
        fileInput2.attr("name", "type"); //设置属性的名字
        fileInput2.attr("value", this.tableType); //设置属性的值
        var fileInput3 = $("<input>");
        fileInput3.attr("type", "hidden");
        fileInput3.attr("name", "deviceId"); //设置属性的名字
        fileInput3.attr("value", this.deviceId); //设置属性的值
        form.append(fileInput1);
        form.append(fileInput2);
        form.append(fileInput3);
        $("body").append(form); //将表单放置在web中
        form.submit(); //表单提交
	}
	// 客户端分页
	client_pages(start,length){
        this.resultTableData = this.tableData.slice(start,start+length);
    }
    //分页回调
    paginate(e){
        this.curPage = e.page+1;
        this.pageSize = e.rows;
        this.client_pages(e.first,e.rows);
    }
	//删除
	del_row(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("logerror/deleteLoadingErrorLog.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message)
				this.load();
			})
		})
	}
	
}